package start;

import decoder.AudioStreamDecoder;
import ffmpeg.FFmpeg;
import util.Common;
import org.apache.log4j.Logger;
import org.bytedeco.javacpp.avcodec;


import static org.bytedeco.javacpp.avformat.avformat_find_stream_info;
import static org.bytedeco.javacpp.avformat.avformat_open_input;

public class VideoDecoder {

    private static final Logger logger = Logger.getLogger(VideoDecoder.class);
    public static void main(String[] args) {
        //TODO 执行耗时操作
        AudioStreamDecoder audioStreamDecoder = new AudioStreamDecoder();

        FFmpeg ffmpeg  = new FFmpeg();
        ffmpeg.open("F:\\Work\\JavaProject\\rtmpServer\\src\\main\\java\\start\\test11.mp4");
        byte[] adts = ffmpeg.getExtradata(ffmpeg.getAvStreamIndex().getAudioStreamIndex());
        byte[] head = new byte[]{(byte) 0xAF, 0x00};
        byte[] headData = Common.arrayMerge(head, adts);
        audioStreamDecoder.decode(headData);
        while (true) {
            avcodec.AVPacket avPacket = ffmpeg.getAvPacket();
            if (avPacket != null) {
                if (avPacket.stream_index() == ffmpeg.getAvStreamIndex().getAudioStreamIndex()) {
                  byte[] head2 = new byte[]{(byte) 0xAF, 0x01};
                  byte[] audioData = new byte[avPacket.size()];
                  avPacket.data().get(audioData);
                  audioData = Common.arrayMerge(head2, audioData);
                  audioStreamDecoder.decode(audioData);
                }
            } else {
                break;
            }
        }
    }
}
